// 1. 安装并导入
import { createRouter, createWebHashHistory } from 'vue-router'

// 2. 创建实例
const router = createRouter({
  // vue3中路由必须指定模式
  history: createWebHashHistory(),
  scrollBehavior(to, from, savedPosition) {
    // to => 到哪去  from => 从哪来  savedPosition => 滚动距离
    // console.log('to', to, 'from', from, 'savedPosition', savedPosition)

    // 回退时保留滚动位置
    // if (savedPosition) {
    //   return savedPosition
    // }

    return {
      top: 0 // 始终回到顶部
      // behavior: 'smooth'  // 滚动效果
    }
  },
  routes: [
    {
      path: '/playground',
      component: () => import('@/views/Playground/index.vue')
    },
    { path: '/login', component: () => import('@/views/Login/index.vue') },
    {
      path: '/',
      component: () => import('@/views/Layout/index.vue'),
      children: [
        { path: '', component: () => import('@/views/Home/index.vue') },
        {
          path: '/category/:id',
          component: () => import('@/views/Category/index.vue')
        },
        {
          path: '/category/sub/:id',
          component: () => import('@/views/Category/sub.vue')
        },
        {
          path: '/goods/:id',
          component: () => import('@/views/Goods/index.vue')
        }
      ]
    }
  ]
})

// 3. 导出并挂载 => 4. 指定路由出口
export default router
